﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using jQueryDotNetLib.Controls;

namespace jQueryDotNetLib.Extenders
{
    [ToolboxItem(false)]
    public class BaseInteractionExtender : Control
    {
        public string Interaction { get; set; }
        public string ConstructorParameters { get; set; }
        public List<string> Parameters { get; set; }
        public List<string> BindingEvents { get; set; }

        [Bindable(true)]
        [Category("Data")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Selector
        {
            get
            {
                if (ViewState["Selector"] == null) return "";
                return (string)ViewState["Selector"];
            }
            set
            {
                ViewState["Selector"] = value;
            }
        }


        protected void AddParameter(string Key, string Parameter)
        {
            if (Parameters == null) Parameters = new List<string>();
            if (Parameter != null && Parameter != "" && Key != null)
            {
                Parameters.Add(((Key != "") ? Key + ": " : "") + Parameter);
            }
        }
        protected void AddBinding(string Event, string EventHandle)
        {
            if (BindingEvents == null) BindingEvents = new List<string>();
            if (EventHandle != "") BindingEvents.Add("'" + Event + "', function(event, ui) {" + EventHandle + "}");
        }

        string resolvedSelector = null;
        public string ResolvedSelector
        {
            get
            {
                if (resolvedSelector == null) 
                    resolvedSelector = jQueryDotNetLib.Utility.ResolveSelectors(this.Selector, ((Page.Master != null) ? Page.Master.Controls : Page.Controls));

                return resolvedSelector;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            if (!DesignMode)
            {
                writer.Write("<script type=\"text/javascript\">");
                writer.Write("jQuery(document).ready(function($) { var ctl = jQuery(\"" +
                    ResolvedSelector + "\"); ctl." + Interaction + "( " + ((ConstructorParameters != null && ConstructorParameters != "") ? ConstructorParameters + "," : "") + 
                    " {");
                if (Parameters != null && Parameters.Count > 0)
                {
                    int count = Parameters.Count;
                    int x = 0;
                    foreach (string p in Parameters)
                    {
                        writer.Write(p);
                        x++;
                        if (x < count) writer.Write(",");
                    }
                }
                writer.Write("} );");
                if (BindingEvents != null && BindingEvents.Count > 0)
                {
                    foreach (string evt in BindingEvents)
                    {
                        writer.Write("ctl.bind(" + evt + ");");
                    }
                }
                writer.Write("});</script>");
            }
        }
    }
}